package com.xuebank.nfe.nfecar.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;


/**
 * Created by Wind_Fantasy on 2016/11/8.
 */

public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private static String TAG = BaseRecyclerViewHolder.class.getSimpleName();

    private final SparseArray<View> mViews;
    private OnItemClickListener onItemClickListener;

    public BaseRecyclerViewHolder(View itemView) {
        super(itemView);
        this.mViews = new SparseArray<>();
    }

    public BaseRecyclerViewHolder(View itemView, OnItemClickListener onItemClickListener) {
        super(itemView);
        this.mViews = new SparseArray<>();
        this.onItemClickListener = onItemClickListener;
        itemView.setOnClickListener(this);
    }

    public boolean isHadItemClickListener() {
        return onItemClickListener != null;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        itemView.setOnClickListener(this);
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null && null != itemView) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(v, getAdapterPosition());
        }
    }

}
